package com.briup.web.controller.part4;

import com.briup.bean.Teacher;
import com.briup.service.ITeacherService;
import com.briup.service.TeacherServiceImpl;
import com.briup.util.Result;
import com.briup.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService service;

    @RequestMapping("/findTeacherById")
    @ResponseBody
    public Result findTeacherById(Integer id){
        //1.调用service方法
        Teacher t = null;
        try {
            t = service.findTeacherById(id);
        } catch (Exception e) {
            //打印异常信息
            e.printStackTrace();
            //获取异常信息
            String msg = e.getMessage();
            //如果是sqlEX ,只要提示错误即可
            if(e instanceof SQLException){
                return new Result(2,"联系管理员");
            }

            return new Result(2,msg);
           // return Result.failure(ResultCode.DATA_EXISTED);
        }
        return Result.success(t);
    }
    public void addTeacher(){
        service.addTeacher(null);
    }
}
